/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.manager;

import com.google.inject.Inject;
import com.wideplay.warp.persist.Transactional;
import exatrez.dao.GroupDAO;
import exatrez.model.Groups;
import exatrez.model.Vessel;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author wan.fairul
 */
public class GroupManager {

    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    @Inject
    private GroupDAO groupDAO;

    @Transactional
    public void saveOrUpdate(Groups group) {
        groupDAO.saveOrUpdate(group);
    }

    @Transactional
    public void deleteGroup(Groups group) {
        groupDAO.delete(group);
    }

    public List<Groups> findAll() {
        String query = "SELECT a FROM Groups a WHERE a.groupId is not null  ";
        Query q = sessionProvider.get().createQuery(query);
        return q.list();
    }

    public Groups findGroupName(String groupName) {
        String query = "SELECT a FROM Groups a WHERE a.groupName = :groupName";
        Query q = sessionProvider.get().createQuery(query);
        q.setString("groupName", groupName);
        return (Groups) q.uniqueResult();
    }
    
    public Groups findGroupIsnotGroupName(String groupId,String groupName) {
        String query = "SELECT a FROM Groups a WHERE a.groupId != :groupId and a.groupName = :groupName";
        Query q = sessionProvider.get().createQuery(query);
        q.setString("groupId", groupId);
        q.setString("groupName", groupName);
        return (Groups) q.uniqueResult();
    }
  
    public GroupDAO getGroupDAO() {
        return groupDAO;
    }

    public void setGroupDAO(GroupDAO groupDAO) {
        this.groupDAO = groupDAO;
    }
}
